package sslSocketFactory;

import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import com.cqtczh.qhd.driver.R;


import android.content.Context;
import android.util.Log;

/** * @author  作者 陈修园: 
 * @date 创建时间：2015-5-25 下午11:49:46 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */
public class CustomerSocketFactory extends SSLSocketFactory {

	private static final String PASSWORD="100110";
	
	
	public CustomerSocketFactory(KeyStore truststore)
			throws NoSuchAlgorithmException, KeyManagementException,
			KeyStoreException, UnrecoverableKeyException {
		super(truststore);
		// TODO Auto-generated constructor stub
	}

	public static SSLSocketFactory getSocketFactory(Context context) {
		InputStream inputStream=null;
		try {
			inputStream=context.getResources().openRawResource(R.raw.cert12306);
			KeyStore truStore=KeyStore.getInstance(KeyStore.getDefaultType());
			truStore.load(inputStream,PASSWORD.toCharArray());
			SSLSocketFactory factory=new CustomerSocketFactory(truStore);
			return factory;			
		} catch (Exception e) {
			// TODO: handle exception
			 Log.d("CustomerSocketFactory", e.getMessage());
			e.printStackTrace();
			//
		}finally{
			if (inputStream!=null) {
				try {
					inputStream.close();
				} catch (Exception e2) {
					// TODO: handle exception
					 Log.d("CustomerSocketFactory", e2.getMessage());
					e2.printStackTrace();
				}
				inputStream=null;
			}
		}		
		return null;
	}
	
	
	public static HttpClient getHttpClient(Context context) {
		BasicHttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params,
				HTTP.DEFAULT_CONTENT_CHARSET);
		HttpProtocolParams.setUseExpectContinue(params, true);
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		SSLSocketFactory sslsocketfactory = CustomerSocketFactory
				.getSocketFactory(context);
		schemeRegistry.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		schemeRegistry.register(new Scheme("https", sslsocketfactory,
				443));
		ClientConnectionManager connMgr = new ThreadSafeClientConnManager(
				params, schemeRegistry);
		return new DefaultHttpClient(connMgr, params);
	}
	
}
